package com.easydev.faseboot.web.config.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.util.StringUtils;

/**
 * @author sxj
 * @date 2018/9/30
 */
public class FBShiroRealm extends AuthorizingRealm {

    /**
     * 授权
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        if (StringUtils.isEmpty(authenticationToken.getPrincipal()) || StringUtils.isEmpty(authenticationToken.getCredentials())) {
            return null;
        }

        if ("test".equals(authenticationToken.getPrincipal())) {
            String md5 = new SimpleHash("md5", "111111".toCharArray(), null, 2).toString();
//            return new SimpleAuthenticationInfo("test", "111111", ByteSource.Util.bytes("easyboot"), getName());
            return new SimpleAuthenticationInfo("test", md5, null, getName());

        }
        return null;
    }
}
